package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: ama
 * Date: 09/10/13
 * Time: 5:33 PM
 * <p/>
 * This class understands how to manage books in the library.
 */
public class BibliotecaLibrary {

    private static ArrayList<Book> books;
    private static ArrayList<Movie> movies;
    private final int MAX_NUMBER_OF_MOVIES = 15;

    public BibliotecaLibrary() {
        books = new ArrayList<Book>();
        movies = new ArrayList<Movie>();
        constructLibrary();
    }

    public void addBooksToLibrary(Book newBook) {
        books.add(newBook);
    }

    public void addMoviesToLibrary(Movie movie) {
        if (movies.size() > MAX_NUMBER_OF_MOVIES) {
            return;
        }
        movies.add(movie);
    }

    public String getBooksInLibrary() {
        String libraryBooks = "";//"Title,\t\t Author\n";
        for (Book bookInLibrary : books) {
            libraryBooks = String.format("%s%s\n", libraryBooks, bookInLibrary.toString());
        }
        return libraryBooks;
    }

    public String getMoviesInLibrary() {
        String libraryMovies = "Title\tYear\tDirector\tRatings\n";
        for (Movie movieInLibrary : movies) {
            libraryMovies = String.format("%s%s\n", libraryMovies, movieInLibrary.toString());
        }
        return libraryMovies;
    }

    public String reserveBook(String selectedBookTitle) {
        if (isBookAvailable(selectedBookTitle)) {
            return "Thank You! Enjoy the book.";
        }
        return "Sorry we don't have that book yet.";

    }

    public boolean isBookAvailable(String selectedBookTitle) {
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(selectedBookTitle)) return true;
        }
        return false;
    }

    public void constructLibrary() {
        addBooksToLibrary(new Book("Head First Java", "Kathy Sierra & Bert Bates"));
        addBooksToLibrary(new Book("Test Driven Development", "Kent Beck"));
        addMoviesToLibrary(new Movie("Sholay", 1975, "Ramesh Sippy"));
    }
}
